#pragma once

#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>



namespace ns_util
{
    
    class TimeUtil
    {
    public:
        // 获得秒级的时间戳
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }
        
        // 获得毫秒级的时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec*1000 + _time.tv_usec/1000);
        }
    };

    class PathUtil
    {
   
    public:
        
        //添加后缀
        static std::string AddSuffix(const std::string &file_name, std::string suffix)
        {
            std::string path_name = "./temp/";
            path_name += file_name;
            path_name += suffix;
            return path_name;
        }

        // 编译时形成的文件
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cc");
        }

        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }

        static std::string Compile_Error(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }
        
        // 运行时形成的文件
        static std::string StdIn(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        
        static std::string StdOut(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }

        static std::string StdErr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public :
        static bool IsFileExists(const std::string &file_name)
        {
            struct stat st;
            if(stat(file_name.c_str(), &st) == 0)
            {
                // 获取属性成功，文件存在
                return true;
            }
            
            return false;
            
        }

        static std::string UniqFileName()
        {
            // 原子性类型
            static std::atomic_uint id(0);
            std::string ms = TimeUtil::GetTimeMs();
            std::string unique_id = std::to_string(id);
            return ms + "-" + unique_id;
        }

        static bool WriteFile(const std::string &target, const std::string &code)
        {
            std::ofstream out(target);
            if(!out.is_open())
            {
                return false;
            }
            out.write(code.c_str(), code.size());
            out.close();
            return true;
        }

        static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
        {

            // // Debug
            // std::cout << "进入ReadFile" <<std::endl;

            (*content).clear();
            std::ifstream in(target);
            if(!in.is_open())
            {
                std::cout << "[util.hpp] [131]"<< "ReadFile函数打开目标文件失败!" << std::endl;
                return false;
            }
            // //Debug
            // std::cout << "debug7" <<std::endl;

            std::string line;
            while(std::getline(in, line))
            {
                (*content) += line;
                (*content) += (keep ? "\n" : "");
            }

            in.close();
            // //Debug
            // std::cout << "debug7" <<std::endl;
            return true;
        }
    };

    class StringUtil
    {
    public:    
        static void SplitString(std::string &line, std::vector<std::string> &tokens, const std::string &seq)
        {
            boost::split(tokens, line, boost::is_any_of(seq), boost::algorithm::token_compress_on);
        }
    };
    
}